package SG3D

import (
	. "SGEngine"
	"github.com/gl"
)

var (
	MaterialMgr materialMgr
)

type materialMgr struct {
	Materials map[string]*Material
}

func (m *materialMgr) AddMaterial(key string, material *Material) {

}

/////////////////////////////////////////////////////////////////
type Material struct {
	Tex0     *ACTexture
	Tex1     *ACTexture
	Color0   *Color4
	Program0 *Program
}

func NewMaterial(pType uint16, imagepath0, imagepath1 string, color0 *Color4) *Material {
	RMaterial := new(Material)
	if imagepath0 != "" {
		RMaterial.Tex0 = ACTextureMgr.LoadTexture(imagepath0)
	}
	if imagepath1 != "" {
		RMaterial.Tex1 = ACTextureMgr.LoadTexture(imagepath1)
	}
	if pType != 0 {
		RMaterial.Program0 = ACProgramMgr.GetProgram(pType)
	}
	if color0 != nil {
		RMaterial.Color0 = color0
	}
	return RMaterial
}

func (m *Material) Use(V, T *gl.Buffer) {

	if m.Tex0 != nil {
		m.Tex0.TexID.Bind(gl.TEXTURE_2D)
	}
	if m.Tex1 != nil {
		m.Tex1.TexID.Bind(gl.TEXTURE_2D)
	}
	m.Program0.ProgramObject.Use()
	V.Bind(gl.ARRAY_BUFFER)
	m.Program0.ProgramObject.GetAttribLocation("vPosition").AttribPointer(3, gl.FLOAT, false, 0, nil)
	m.Program0.ProgramObject.GetAttribLocation("vPosition").EnableArray()
	if T != nil {
		T.Bind(gl.ARRAY_BUFFER)
		m.Program0.ProgramObject.GetUniformLocation("inColor").Uniform4f(1, 1, 1, 1)
		m.Program0.ProgramObject.GetAttribLocation("a_texCoord").AttribPointer(2, gl.FLOAT, false, 0, nil)
		m.Program0.ProgramObject.GetAttribLocation("a_texCoord").EnableArray()
	}
}
